﻿using System.Windows;
using System.Windows.Controls;

namespace CustomBusyIndicator.Controls
{
    public class BusyIndicator : Control
    {
        public bool IsBusy
        {
            get { return (bool)GetValue(IsBusyProperty); }
            set { SetValue(IsBusyProperty, value); }
        }

        public static readonly DependencyProperty IsBusyProperty =
            DependencyProperty.Register("IsBusy", typeof(bool), typeof(BusyIndicator), new PropertyMetadata(OnIsBusyChanged));

        private static void OnIsBusyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((BusyIndicator)d).Visibility = (bool)e.NewValue ? Visibility.Visible : Visibility.Collapsed;
            VisualStateManager.GoToState((BusyIndicator)d, (bool)e.NewValue ? "On" : "Off", false);
        }

        public override void OnApplyTemplate()
        {
            this.Visibility = this.IsBusy ? Visibility.Visible : Visibility.Collapsed;
            VisualStateManager.GoToState(this, this.IsBusy ? "On" : "Off", false);
            base.OnApplyTemplate();
        }

        protected override void OnTap(System.Windows.Input.GestureEventArgs e)
        {
            e.Handled = true;
            base.OnTap(e);
        }
    }
}